package com.cliff.joinus.activity;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;
import org.cybergarage.net.HostInterface;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TwoLineListItem;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.cliff.joinus.Constants;
import com.cliff.joinus.JoinusControlPoint;
import com.cliff.joinus.MainApplication;
import com.cliff.joinus.R;

public class InfoActivity extends SherlockListActivity {

	protected final int[][] ACTIONBARMENU = new int[][]{
			new int[]{Constants.ITEM_BACK, android.R.drawable.ic_menu_revert, R.string.menu_item_back},
			new int[]{Constants.ITEM_REFRESH, android.R.drawable.ic_menu_rotate, R.string.menu_item_refresh}
			};
	protected ArrayAdapter<BasicNameValuePair> mAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mAdapter = new ArrayAdapter<BasicNameValuePair>(this,
				android.R.layout.simple_list_item_2, getInfoList()) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				TwoLineListItem row;
				if (convertView == null) {
					LayoutInflater inflater = (LayoutInflater) getApplicationContext()
							.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					row = (TwoLineListItem) inflater.inflate(
							android.R.layout.simple_list_item_2, null);
				} else {
					row = (TwoLineListItem) convertView;
				}
				BasicNameValuePair data = getItem(position);
				row.getText1().setText(data.getName());
				row.getText2().setText(data.getValue());
				return row;
			}
		};

		setListAdapter(mAdapter);
		setTitle(R.string.title_activity_info);		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
			
		MainActivity.initMenu(menu, ACTIONBARMENU);

		// return super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (android.R.id.home == item.getItemId()) {
			finish();
			return true;
		}
		
		if (Constants.GROUP_COMMON == item.getGroupId()) {
			switch(item.getItemId()) {
			case Constants.ITEM_BACK:
				finish();
				break;
			case Constants.ITEM_REFRESH:
				mAdapter.clear();
				ArrayList<BasicNameValuePair> infoList = getInfoList();
				for (BasicNameValuePair infoItem:infoList) 
					mAdapter.add(infoItem);
				mAdapter.notifyDataSetChanged();
				break;
			}
		}

//		return super.onOptionsItemSelected(item);
		return true;
	}

	protected ArrayList<BasicNameValuePair> getInfoList() {		
		
		String infoStr = "";
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		JoinusControlPoint cp = JoinusControlPoint.getInstance();
		MainApplication app = (MainApplication)getApplication();

		//ssid
		list.add(getInfo(R.string.info_wifi_ssid_name, app.getSSIDName()));
		//ip
		infoStr = HostInterface.getIPv4Address();
		infoStr = ("".equals(infoStr))?getString(R.string.hint_none):infoStr;
		list.add(getInfo(R.string.info_ip, infoStr));
		//control point status
		list.add(getInfo(R.string.info_cp_status, cp.isRunning()?R.string.status_running:R.string.status_stopped));
		//httpd status
		list.add(getInfo(R.string.info_httpd_status, app.isHttpdRunning()?R.string.status_running:R.string.status_stopped));
		
		return list;
	}
	
	protected BasicNameValuePair getInfo(int nameResId, String value) {
		return new BasicNameValuePair(getString(nameResId), value);
	}

	protected BasicNameValuePair getInfo(int nameResId, int valueResId) {
		return new BasicNameValuePair(getString(nameResId), getString(valueResId));
	}
	
}
